<template>
  <section>
    <van-sticky>
      <PageHeader title="角色管理"/>
    </van-sticky>

    <div class="pd10">
      <van-button type="primary" class="w100" @click="$router.push({name: 'RoleAdd'})">+添加</van-button>
    </div>

    <van-list
      class="mt10"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="requestData">
      <van-row class="list-item bg-color-white">
        <van-col :span="8" class="pl10 text-left strong">名称</van-col>
        <van-col :span="10" class="pl10 text-left strong">备注</van-col>
        <van-col :span="6" class="pr10 text-right strong">操作</van-col>
      </van-row>

      <van-row v-for="(item, index) in dataList" :key="index" class="list-item bg-color-white">
        <van-col :span="8" class="pl10 text-left">{{item.name}}</van-col>
        <van-col :span="10" class="pl10 text-left">{{item.remark}}</van-col>
        <van-col :span="6" class="pr10 text-right">
          <van-icon name="edit" @click="$router.push({name: 'RoleEdit', params: {id: item.id}})" />
          <van-icon name="delete" class="ml10" @click="del(index)" />
          <van-icon name="warning" class="ml10" @click="$router.push({name: 'RoleResources', params: {roleId: item.id}})" />
        </van-col>
      </van-row>
    </van-list>
  </section>
</template>

<script>
import {roleListsUri, roleDeleteUri} from "../../common/api";
import PageHeader from "../componets/PageHeader";
import Vue from 'vue';
import {Toast, Dialog} from 'vant';
Vue.use(Toast).use(Dialog);

export default {
  components: {
    PageHeader
  },
  data() {
    return {
      currentPage: 1,
      lastPage: 0,
      total: 0,
      pageSize: 10,
      loading: false,
      finished: false,
      dataList: []
    }
  },
  methods: {
    del(index) {
      const that = this;
      Dialog.confirm({
        message: '确定要删除吗？'
      }).then(() => { // on confirm
        that.$http.get(roleDeleteUri + '?id=' + that.dataList[index].id).then(response => {
          let {code, msg, data} = response.body
          if (code != 0) {
            Toast.fail(msg);
          } else {
            that.dataList.splice(index, 1);
          }
        }, response => {
          // error callback
        })
      }).catch(() => {  // on cancel

      });
    },
    requestData() {
      const that = this;
      this.loading = true;
      const url = roleListsUri + '?page_size=' + that.pageSize + '&page=' + that.currentPage;
      this.$http.get(url).then(response => {
        let {code, msg, data} = response.body
        if (code != 0) {
          Toast.fail(msg);
        } else {
          that.loading = false;
          if (that.currentPage >= data.last_page) {
            that.finished = true;
          }
          that.currentPage = that.currentPage + 1;
          for (let i = 0; i < data.data.length; i++) {
            that.dataList.push(data.data[i]);
          }
          that.total = data.total;
          that.lastPage = data.last_page;
        }
      }, response => {
        // error callback
      })
    }
  },
  mounted: function () {

  }
}
</script>

<style scoped>

</style>
